package com.min.generator.config;

import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
import com.baomidou.mybatisplus.core.incrementer.IdentifierGenerator;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @author fengt
 */
@Data
@Configuration
@ConfigurationProperties(prefix = "generator", ignoreInvalidFields = true)
public class GeneratorConfig {

    private String mainPath;
    private String moduleName;
    private String packageName;
    private String author;
    private String email;
    private String tablePrefix;
    private Set<String> ignoreField;

    /**
     * 生成的文件存储的绝对路径
     * @author: fengt
     * @date: 2021/3/19 12:46
     **/
    private String FILE_PATH = null;
    public String getFilePath(String type, String className) {

        String absSourcePath = StrUtil.format("{}{}main{}java{}{}",
                this.mainPath,
                File.separator,
                File.separator,
                File.separator,
                this.packageName.replace(".", File.separator));

        String absResourcePath = StrUtil.format("{}{}main{}resources",
                this.mainPath,
                File.separator,
                File.separator);

        FILE_PATH = null;
        switch (type) {
            case "entity":
                FILE_PATH = StrUtil.format("{}{}entity{}{}Entity.java", absSourcePath, File.separator, File.separator, className);
                break;
            case "dao":
                FILE_PATH = StrUtil.format("{}{}dao{}{}Dao.java", absSourcePath, File.separator, File.separator, className);
                break;
            case "service":
                FILE_PATH = StrUtil.format("{}{}service{}{}Service.java", absSourcePath, File.separator, File.separator, className);
                break;
            case "controller":
                FILE_PATH = StrUtil.format("{}{}controller{}{}Controller.java", absSourcePath, File.separator, File.separator, className);
                break;
            case "mapper":
                FILE_PATH = StrUtil.format("{}{}mapper{}{}Mapper.xml", absResourcePath, File.separator, File.separator, className);
                break;
            case "vue_main":
                FILE_PATH = StrUtil.format("{}{}views{}index.vue", absResourcePath, File.separator, File.separator);
                break;
            case "vue_add":
                FILE_PATH = StrUtil.format("{}{}views{}{}-add-update.vue", absResourcePath, File.separator, File.separator, className.toLowerCase());
                break;
            case "vue_api":
                FILE_PATH = StrUtil.format("{}{}views{}{}-api.js", absResourcePath, File.separator, File.separator, className.toLowerCase());
                break;
            case "sql":
                FILE_PATH = StrUtil.format("{}{}sql{}{}.sql", absResourcePath, File.separator, File.separator,className.toLowerCase());
                break;
            default:
                break;

        }

        return FILE_PATH;
    }

    /**
     * 压缩到Zip文件里的相对路径
     * @author: fengt
     * @date: 2021/3/19 12:49
     **/
    private Map<String, String> ZIP_PATH = null;
    public Map<String, String> getZipPath(String className) {
        ZIP_PATH = MapUtil.builder(new HashMap<String, String>())
                .put("entity", StrUtil.format("entity{}{}Entity.java", File.separator, className))
                .put("dao", StrUtil.format("dao{}{}Dao.java", File.separator, className))
                .put("service", StrUtil.format("service{}{}Service.java", File.separator, className))
                .put("controller", StrUtil.format("controller{}{}Controller.java", File.separator, className))
                .put("mapper", StrUtil.format("mapper{}{}Mapper.xml", File.separator, className))
                .put("vue_main", StrUtil.format("views{}{}{}index.vue", File.separator, className, File.separator))
                .put("vue_add", StrUtil.format("views{}{}{}{}-add-update.vue", File.separator, className, File.separator, className.toLowerCase()))
                .put("vue_api", StrUtil.format("views{}{}{}{}-api.js", File.separator, className, File.separator, className.toLowerCase()))
                .put("sql", StrUtil.format("{}_menu.sql", className.toLowerCase()))
                .build();
        return ZIP_PATH;
    }

    @Bean
    public IdentifierGenerator keyGenerator() {
        return new DefaultIdentifierGenerator();
    }

}
